
#include "../main.h"
#include "base64.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "esp_bt.h"

#include "esp_blufi_api.h"
#include "gatt.h"

#include "esp_blufi.h"
#include "../aws_esp32/aws_iot_Publish.h"

static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param);

#define WIFI_LIST_NUM   10

static wifi_config_t sta_config;
static wifi_config_t ap_config;

static bool gl_sta_connected = false;
static bool ble_is_connected = false;
static uint8_t gl_sta_bssid[6];
static uint8_t gl_sta_ssid[32];
static int gl_sta_ssid_len;
char Ble_RxBuf[200];
uint16_t Ble_RxCount;
char gatt_SSID[200];
char gatt_Password[200];

//WIFI名称和密码 不能超过32个字符
#define BlueRx_Length 16
#define BlueRx_MaxPackSeg 7
uint8_t BlueRx_PackSeg = 0;
typedef struct 
{
    uint8_t packSeq; //0~5
    uint8_t packSum; //1~5
    uint8_t pack[BlueRx_Length-2];
}BlueData_Pack_S;
typedef union
{
    BlueData_Pack_S BlueRx_Pack;
    uint8_t BlueRx_PackBuf[BlueRx_Length];
}BlueRx_Buf_U; //单包
BlueRx_Buf_U BlueRx_Buf[BlueRx_MaxPackSeg]; //BlueRx_MaxPackSeg个数据包
uint8_t BlueData_Spelling[108]; //16*7

typedef enum
{
    Ble_Mode_None,
    Ble_Mode_ConfigWIFI,
    Ble_Mode_CommunicationCtrl,
}BlueOperMode_E;
BlueOperMode_E BlueOperMode = Ble_Mode_None;
uint16_t seqSSID = 0;
uint16_t seqPassword = 0;
uint16_t subPack_Length = 0;
uint8_t isSub_SSID = 0;
uint8_t isSub_Pass = 0;
uint8_t pPayload[256];


void Blue_RxBuf_Reset(void)
{
    BlueRx_PackSeg = 0;
    for (int i = 0; i < 108; i++)
    {
        BlueData_Spelling[i] = 0;
    }
    for(int i = 0; i < 200; i++)
    {
        Ble_RxBuf[i] = 0;
        gatt_SSID[i] = 0;
        gatt_Password[i] = 0;

        pPayload[i] = 0;
    }
    Ble_RxCount = 0;
    seqSSID = 0;
    seqPassword = 0;
    subPack_Length = 0;
}

static esp_blufi_callbacks_t example_callbacks = {
    .event_cb = example_event_callback,
    .negotiate_data_handler = blufi_dh_negotiate_data_handler,
    .encrypt_func = blufi_aes_encrypt,
    .decrypt_func = blufi_aes_decrypt,
    .checksum_func = blufi_crc_checksum,
};

void Ble_Send(uint8_t* data, uint32_t data_len)
{
    while(esp_get_free_heap_size() < 5*1025)
    {
        vTaskDelay(pdMS_TO_TICKS(400));
        BLUFI_INFO("当前可用内存低于 5k, 延迟100ms 发送蓝牙数据");
    }
    esp_blufi_send_custom_data(data,data_len); //btc_blufi_send_notify
    // esp_blufi_send_notify((uint8_t*)data);
}

void gatt_wifi_state(uint8_t state)
{
    char dat_Val[100];
    extern volatile unsigned int ID_first_four; //MAC初4位做识别
    extern volatile unsigned int ID_mid_four;   //MAC中间4位做识别
    extern volatile unsigned int ID_last_four;  //MAC最后4位做识别

    BlueOperMode = Ble_Mode_None;
    if(ble_is_connected == false) return;
    if(state > 0)
    {
        BLUFI_INFO("blufi send yes");
        sprintf((char*)dat_Val, "yes %04x%04x%04x", ID_first_four, ID_mid_four, ID_last_four);
        Ble_Send((uint8_t*)dat_Val,strlen((char*)dat_Val));
    }
    else
    {
        BLUFI_INFO("blufi send no");
        Ble_Send((uint8_t*)"no",2);
    }
}

void ble_ssid(unsigned char* str, uint16_t str_len)
{
    int ret = strncmp((char*)str, "ssid ", 5);
    if(ret == 0)
    {
        BlueOperMode = Ble_Mode_ConfigWIFI;
        for(int i = 0; i < 200; i++)
        {
            if(i < (str_len - 5)) gatt_SSID[i] = str[i + 5];
            else gatt_SSID[i] = 0;
        }
        g_wifi_ssid = gatt_SSID;
        BLUFI_INFO("WIFI ssid: %s",g_wifi_ssid);
    }
}
void ble_password(unsigned char* str, uint16_t str_len)
{
    int ret = strncmp((char*)str, "password ", 9);
    if(ret == 0)
    {
        BlueOperMode = Ble_Mode_ConfigWIFI;
        for(int i = 0; i < 200; i++)
        {
            if(i < (str_len - 9)) gatt_Password[i] = str[i + 9];
            else gatt_Password[i] = 0;
        }
        g_wifi_password = gatt_Password;
        BLUFI_INFO("WIFI password: %s",g_wifi_password);

        WIFI_Connect_Trigger();
    }
}
void gatt_blue_config_wifi(unsigned char* str, uint16_t base64_len)
{
    unsigned char txt[200];
    unsigned char subPack[200];
    uint16_t i = 0;

    for(i = 0; i < 200; i++)
    {
        txt[i] = 0;
        subPack[i] = 0;
    }
    //将base64解码成包含特殊字符的字符串
    b64_decode(str,base64_len,txt);
    int txtLength = strlen((char*)txt);

    if((strncmp((char*)txt, "subSSID ", 8) == 0) || (strncmp((char*)txt, "subPass ", 8) == 0))
    {
        for(i = 0; i < (txtLength - 8); i++)
        {
            subPack[i] = txt[i + 8];
        }
        subPack_Length = atoi((char*)subPack);
        BLUFI_INFO("分包处理, 总数据长度: %d",subPack_Length);
        if(strncmp((char*)txt, "subSSID ", 8) == 0) isSub_SSID = 1;
        else if(strncmp((char*)txt, "subPass ", 8) == 0) isSub_Pass = 1;
        BlueOperMode = Ble_Mode_ConfigWIFI;

        return;
    }
    
    if(subPack_Length == 0)
    {
        ble_ssid((unsigned char*)txt, txtLength);
    }
    else if(isSub_SSID > 0)
    { //ssid 分包处理
        for(i = 0; i < base64_len; i++)
        {
            gatt_SSID[seqSSID] = str[i];
            seqSSID++;
        }
        if(seqSSID >= subPack_Length)
        {
            for(i = 0; i < 200; i++)
            {
                txt[i] = 0;
                subPack[i] = 0;
            }
            b64_decode((unsigned char*)gatt_SSID,seqSSID,txt);

            int txtLength = strlen((char*)txt);
            BLUFI_INFO("%s, txtLength: %d",txt, txtLength);
            ble_ssid((unsigned char*)txt, txtLength);
            seqSSID = 0;
            isSub_SSID = 0;
            subPack_Length = 0;
        }
    }
    
    if(subPack_Length == 0)
    {
        ble_password((unsigned char*)txt, txtLength);
    }
    else if(isSub_Pass > 0)
    { //password 分包处理
        for(i = 0; i < base64_len; i++)
        {
            gatt_Password[seqPassword] = str[i];
            seqPassword++;
        }
        if(seqPassword >= subPack_Length)
        {
            for(i = 0; i < 200; i++)
            {
                txt[i] = 0;
                subPack[i] = 0;
            }
            b64_decode((unsigned char*)gatt_Password,seqPassword,txt);

            int txtLength = strlen((char*)txt);
            BLUFI_INFO("%s, txtLength: %d",txt, txtLength);
            ble_password((unsigned char*)txt, txtLength);
            seqPassword = 0;
            isSub_Pass = 0;
            subPack_Length = 0;
        }
    }
}
esp_err_t ble_receive_command(char* str, int blen)
{
    uint8_t buf[18];
    MqttSubscribe_Msg_S msg;
    uint8_t temp;

    if(BlueOperMode == Ble_Mode_ConfigWIFI) return ESP_OK;
    if(ble_is_connected == false) return ESP_OK;
    
    //解码订阅
    for(int i = 0; i < 18; i++) buf[i] = 0;
    for (int i = 0; i < 2; i++) buf[i] = str[i];
    temp = (buf[0] - 0x30) * 10 + (buf[1] - 0x30);
    for(int i = 0; i < 18; i++) buf[i] = 0;
    if(((temp >= SubMsgID_Poweron) && (temp <= SubMsgID_SetAll)) && (str[2] == 0x20))
    {
        for (int i = 3; i < blen; i++)
        {
            buf[i - 3] = str[i];
        }
        msg.MqttSubscribe_MsgID = temp;
        msg.result = atoi((char*)buf);
        ESP_LOGE("蓝牙 解析参数ID", "%d", msg.MqttSubscribe_MsgID);
        ESP_LOGE("蓝牙 解析参数", "%d", msg.result);
        Subscribe_CmdHandle(msg);
    }

    //编码发布
    for (int i = 0; i < 256; i++)
    {
        pPayload[i] = 0x20;
    }
    
    pPayload[0] = 0x30 +1;
    for (int i = 0; i < 6; i++)
    {
        pPayload[1 + i] = 0x30 + AWS_IoT_PublishMsg.PubMsg_ID[i];
    }
    pPayload[7] = 0x30 + AWS_IoT_PublishMsg.PubMsg_Firmware;
    pPayload[8] = 0x30 + AWS_IoT_PublishMsg.PubMsg_OTA;
    pPayload[9] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Squ >> 24) & 0xff);
    pPayload[10] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Squ >> 16) & 0xff);
    pPayload[11] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Squ >> 8) & 0xff);
    pPayload[12] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_Squ & 0xff);
    pPayload[13] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Set[0] >> 24) & 0xff);
    pPayload[14] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Set[0] >> 16) & 0xff);
    pPayload[15] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Set[0] >> 8) & 0xff);
    pPayload[16] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_Timer_Set[0] & 0xff);
    Ble_Send(pPayload, 17);

    pPayload[0] = 0x30 +2;
    pPayload[1] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Set[1] >> 24) & 0xff); //0x30 + : 将值类型转ASCII字符
    pPayload[2] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Set[1] >> 16) & 0xff);
    pPayload[3] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Set[1] >> 8) & 0xff);
    pPayload[4] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_Timer_Set[1] & 0xff);
    pPayload[5] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Rem[0] >> 24) & 0xff);
    pPayload[6] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Rem[0] >> 16) & 0xff);
    pPayload[7] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Rem[0] >> 8) & 0xff);
    pPayload[8] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_Timer_Rem[0] & 0xff);
    pPayload[9] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Rem[1] >> 24) & 0xff);
    pPayload[10] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Rem[1] >> 16) & 0xff);
    pPayload[11] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Timer_Rem[1] >> 8) & 0xff);
    pPayload[12] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_Timer_Rem[1] & 0xff);
    pPayload[13] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Coundown >> 8) & 0xff);
    pPayload[14] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_Coundown) & 0xff);
    pPayload[15] = (AWS_IoT_PublishMsg.PubMsg_State) & 0xf;
    pPayload[15] <<= 4;
    pPayload[15] |= (AWS_IoT_PublishMsg.PubMsg_Err) & 0xf;
    pPayload[15] = 0x30 + pPayload[15];
    pPayload[16] = AWS_IoT_PublishMsg.PubMsg_IsHeat;
    pPayload[16] <<= 4;
    pPayload[16] |= AWS_IoT_PublishMsg.PubMsg_Unit;
    pPayload[16] = 0x30 + pPayload[16];
    Ble_Send(pPayload, 17); //该函数只能发字符串, 不能按字节发送,例如: 8位变量值为0, 在字符串是结束符号,导致无法将数字0传送出去

    pPayload[0] = 0x30 + 3;
    pPayload[1] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[0] >> 24) & 0xff);
    pPayload[2] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[0] >> 16) & 0xff);
    pPayload[3] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[0] >> 8) & 0xff);
    pPayload[4] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_RTD_Set[0] & 0xff);
    pPayload[5] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[1] >> 24) & 0xff);
    pPayload[6] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[1] >> 16) & 0xff);
    pPayload[7] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[1] >> 8) & 0xff);
    pPayload[8] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_RTD_Set[1] & 0xff);
    pPayload[9] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[2] >> 24) & 0xff);
    pPayload[10] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[2] >> 16) & 0xff);
    pPayload[11] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Set[2] >> 8) & 0xff);
    pPayload[12] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_RTD_Set[2] & 0xff);
    pPayload[13] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[0] >> 24) & 0xff);
    pPayload[14] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[0] >> 16) & 0xff);
    pPayload[15] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[0] >> 8) & 0xff);
    pPayload[16] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_RTD_Temp[0] & 0xff);
    Ble_Send(pPayload, 17);

    pPayload[0] = 0x30 + 4;
    pPayload[1] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[1] >> 24) & 0xff);
    pPayload[2] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[1] >> 16) & 0xff);
    pPayload[3] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[1] >> 8) & 0xff);
    pPayload[4] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_RTD_Temp[1] & 0xff);
    pPayload[5] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[2] >> 24) & 0xff);
    pPayload[6] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[2] >> 16) & 0xff);
    pPayload[7] = 0x30 + ((AWS_IoT_PublishMsg.PubMsg_RTD_Temp[2] >> 8) & 0xff);
    pPayload[8] = 0x30 + (AWS_IoT_PublishMsg.PubMsg_RTD_Temp[2] & 0xff);
    Ble_Send(pPayload, 9);
    vTaskDelay(500 / portTICK_RATE_MS); //任务延迟 

    return ESP_OK;
}

static void example_event_callback(esp_blufi_cb_event_t event, esp_blufi_cb_param_t *param)
{
    switch (event) 
    {
        case ESP_BLUFI_EVENT_INIT_FINISH:
            BLUFI_INFO("蓝牙初始化完成\n");
            esp_blufi_adv_start();
            break;
        case ESP_BLUFI_EVENT_DEINIT_FINISH:
            BLUFI_INFO("蓝牙删除初始化完成\n");
            break;
        case ESP_BLUFI_EVENT_BLE_CONNECT:
            BLUFI_INFO("蓝牙连接 OK\n");
            BlueOperMode = Ble_Mode_None;
            ble_is_connected = true;
            Blue_RxBuf_Reset();
            esp_blufi_adv_stop();
            blufi_security_init();
            break;
        case ESP_BLUFI_EVENT_BLE_DISCONNECT:
            BLUFI_INFO("蓝牙 已断开\n");
            ble_is_connected = false;
            blufi_security_deinit();
            esp_blufi_adv_start();
            break;
        case ESP_BLUFI_EVENT_SET_WIFI_OPMODE:
            BLUFI_INFO("设置WIFI模式 %d\n", param->wifi_mode.op_mode);
            break;
        case ESP_BLUFI_EVENT_REQ_CONNECT_TO_AP:
            BLUFI_INFO("App请求 蓝牙连接到WIFI\n");
            /* 当设备已连接到此wifi时，没有wifi回调
            所以在连接之前断开无线网络连接。
            */
            esp_wifi_disconnect();
            esp_wifi_connect();
            break;
        case ESP_BLUFI_EVENT_REQ_DISCONNECT_FROM_AP:
            BLUFI_INFO("App请求 蓝牙断开WIFI\n");
            esp_wifi_disconnect();
            break;
        case ESP_BLUFI_EVENT_REPORT_ERROR:
            BLUFI_ERROR("蓝牙请求错误, error code %d\n", param->report_error.state);
            esp_blufi_send_error_info(param->report_error.state);
            break;
        case ESP_BLUFI_EVENT_GET_WIFI_STATUS: {
            wifi_mode_t mode;
            esp_blufi_extra_info_t info;

            esp_wifi_get_mode(&mode);
            if (gl_sta_connected)
            {
                memset(&info, 0, sizeof(esp_blufi_extra_info_t));
                memcpy(info.sta_bssid, gl_sta_bssid, 6);
                info.sta_bssid_set = true;
                info.sta_ssid = gl_sta_ssid;
                info.sta_ssid_len = gl_sta_ssid_len;
                esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_SUCCESS, 0, &info);
            } else {
                esp_blufi_send_wifi_conn_report(mode, ESP_BLUFI_STA_CONN_FAIL, 0, NULL);
            }
            BLUFI_INFO("获取WIFI状态给App\n");

            break;
        }
        case ESP_BLUFI_EVENT_RECV_SLAVE_DISCONNECT_BLE:
            BLUFI_INFO("蓝牙断开。。。");
            esp_blufi_disconnect();
            break;
        case ESP_BLUFI_EVENT_DEAUTHENTICATE_STA:
            break;
        case ESP_BLUFI_EVENT_RECV_STA_BSSID:
            memcpy(sta_config.sta.bssid, param->sta_bssid.bssid, 6);
            sta_config.sta.bssid_set = 1;
            esp_wifi_set_config(WIFI_IF_STA, &sta_config);
            BLUFI_INFO("接收基础SSID %s\n", sta_config.sta.ssid);
            break;
        case ESP_BLUFI_EVENT_RECV_STA_SSID:
            strncpy((char *)sta_config.sta.ssid, (char *)param->sta_ssid.ssid, param->sta_ssid.ssid_len);
            sta_config.sta.ssid[param->sta_ssid.ssid_len] = '\0';
            esp_wifi_set_config(WIFI_IF_STA, &sta_config);
            BLUFI_INFO("接收SSID %s\n", sta_config.sta.ssid);
            break;
        case ESP_BLUFI_EVENT_RECV_STA_PASSWD:
            strncpy((char *)sta_config.sta.password, (char *)param->sta_passwd.passwd, param->sta_passwd.passwd_len);
            sta_config.sta.password[param->sta_passwd.passwd_len] = '\0';
            esp_wifi_set_config(WIFI_IF_STA, &sta_config);
            BLUFI_INFO("接收STA密码 %s\n", sta_config.sta.password);
            break;
        case ESP_BLUFI_EVENT_RECV_SOFTAP_SSID:
            BLUFI_INFO("Recv AP SSID %s, ssid len %d\n", ap_config.ap.ssid, ap_config.ap.ssid_len);
            break;
        case ESP_BLUFI_EVENT_RECV_SOFTAP_PASSWD:
            BLUFI_INFO("Recv AP PASSWORD %s len = %d\n", ap_config.ap.password, param->softap_passwd.passwd_len);
            break;
        case ESP_BLUFI_EVENT_RECV_SOFTAP_MAX_CONN_NUM:
            if (param->softap_max_conn_num.max_conn_num > 4) {
                return;
            }
            ap_config.ap.max_connection = param->softap_max_conn_num.max_conn_num;
            esp_wifi_set_config(WIFI_IF_AP, &ap_config);
            BLUFI_INFO("接收AP最大连接数量 %d\n", ap_config.ap.max_connection);
            break;
        case ESP_BLUFI_EVENT_RECV_SOFTAP_AUTH_MODE:
            if (param->softap_auth_mode.auth_mode >= WIFI_AUTH_MAX) {
                return;
            }
            ap_config.ap.authmode = param->softap_auth_mode.auth_mode;
            esp_wifi_set_config(WIFI_IF_AP, &ap_config);
            BLUFI_INFO("接收AP模式 %d\n", ap_config.ap.authmode);
            break;
        case ESP_BLUFI_EVENT_RECV_SOFTAP_CHANNEL:
            if (param->softap_channel.channel > 13) {
                return;
            }
            ap_config.ap.channel = param->softap_channel.channel;
            esp_wifi_set_config(WIFI_IF_AP, &ap_config);
            BLUFI_INFO("接收Ap channel %d\n", ap_config.ap.channel);
            break;
        case ESP_BLUFI_EVENT_GET_WIFI_LIST:{
            wifi_scan_config_t scanConf = {
                .ssid = NULL,
                .bssid = NULL,
                .channel = 0,
                .show_hidden = false
            };
            // esp_wifi_scan_start(&scanConf, true);
            break;
        }
        case ESP_BLUFI_EVENT_RECV_CUSTOM_DATA:
            // BLUFI_INFO("接收数据: %s %d", param->custom_data.data, param->custom_data.data_len);
            
            gatt_blue_config_wifi((unsigned char*)(param->custom_data.data), param->custom_data.data_len);
            ble_receive_command((char*)(param->custom_data.data), param->custom_data.data_len);

            break;
        case ESP_BLUFI_EVENT_RECV_USERNAME:
            BLUFI_INFO("未处理0\n");
            break;
        case ESP_BLUFI_EVENT_RECV_CA_CERT:
            BLUFI_INFO("未处理1\n");
            break;
        case ESP_BLUFI_EVENT_RECV_CLIENT_CERT:
            BLUFI_INFO("未处理2\n");
            break;
        case ESP_BLUFI_EVENT_RECV_SERVER_CERT:
            BLUFI_INFO("未处理3\n");
            break;
        case ESP_BLUFI_EVENT_RECV_CLIENT_PRIV_KEY:
            BLUFI_INFO("未处理4\n");
            break;;
        case ESP_BLUFI_EVENT_RECV_SERVER_PRIV_KEY:
            BLUFI_INFO("未处理5\n");
            break;
        default:
            break;
    }
}

void Gatt_Init(void)
{
    esp_err_t ret;

    ESP_ERROR_CHECK(esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT));
    esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
    ret = esp_bt_controller_init(&bt_cfg);
    if (ret) 
    {
        BLUFI_ERROR("%s 初始化BT控制器 failed: %s\n", __func__, esp_err_to_name(ret));
    }
    ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
    if (ret)
    {
        BLUFI_ERROR("%s 使能BT控制失败: %s\n", __func__, esp_err_to_name(ret));
        return;
    }
    ret = esp_blufi_host_and_cb_init(&example_callbacks);
    if (ret)
    {
        BLUFI_ERROR("%s 初始化失败: %s\n", __func__, esp_err_to_name(ret));
        return;
    }
    BLUFI_INFO("BLUFI版本 %04x\n", esp_blufi_get_version());
    BLUFI_INFO("蓝牙初始化 完成");

    Blue_RxBuf_Reset();

}
